<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="实验室" prop="labId">
        <!-- <el-input v-model="queryParams.labId" placeholder="请输入实验室id" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select v-model="queryParams.labId" placeholder="请选择实验室名称">
          <el-option :label="item.labName" :value="item.labId" v-for="(item,index) in laboratoryList" :key="index" />
        </el-select>

      </el-form-item>
      <el-form-item label="创建日期" prop="createDate">
        <el-date-picker clearable v-model="queryParams.createDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择创建日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['lab:labrequest:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['lab:labrequest:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['lab:labrequest:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['lab:labrequest:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="labrequestList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="requestId" />
      <el-table-column label="申请人" align="center" prop="nickName" />
      <el-table-column label="实验室名称" align="center" prop="labName" />
      <el-table-column label="学期" align="center" prop="term" />
      <el-table-column label="周次" align="center" prop="week" />
      <el-table-column label="上课节次" align="center" prop="periodName" />
      <el-table-column label="审核状态" align="center" prop="status" />
      <el-table-column label="申请原因描述" align="center" prop="reason" />
      <el-table-column label="创建日期" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['lab:labrequest:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['lab:labrequest:remove']">删除</el-button>
            <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['lab:labrequest:changeStatus']">
                <el-button size="mini" type="text" icon="el-icon-d-arrow-right">修改状态</el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="handleUpdateStatus" icon="el-icon-circle-check"
                    v-hasPermi="['lab:labrequest:changeStatus']">审核状态修改</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改实验室申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50vw" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="8vw">

        <el-form-item label="实验室名称" prop="labId">
          <!-- <el-input v-model="form.labId" placeholder="请输入实验室id" /> -->
          <el-select v-model="form.labId" placeholder="请选择实验室名称">
            <el-option :label="item.labName" :value="item.labId" v-for="(item,index) in laboratoryList" :key="index" />
          </el-select>
        </el-form-item>


        <el-form-item label="学期id" prop="semesterId">
          <el-input v-model="form.semesterId" :placeholder="this.form.semesterId" :disabled="true" />
        </el-form-item>

        <el-form-item label="开始周" prop="startWeek">
          <!-- <el-input v-model="form.startWeek" placeholder="请输入开始周" /> -->
          <el-select v-model="form.startWeek" placeholder="请选择开始周">
            <el-option label="1" value="1"/>
            <el-option label="2" value="2"/>
            <el-option label="3" value="3"/>
            <el-option label="4" value="4"/>
            <el-option label="5" value="5"/>
            <el-option label="6" value="6"/>
            <el-option label="7" value="7"/>
            <el-option label="8" value="8"/>
            <el-option label="9" value="9"/>
            <el-option label="10" value="10"/>
            <el-option label="11" value="11"/>
            <el-option label="12" value="12"/>
            <el-option label="13" value="13"/>
            <el-option label="14" value="14"/>
            <el-option label="15" value="15"/>
            <el-option label="16" value="16"/>
            <el-option label="17" value="17"/>
            <el-option label="18" value="18"/>
          </el-select>
        </el-form-item>
        <el-form-item label="结束周" prop="endWeek">
          <!-- <el-input v-model="form.endWeek" placeholder="请输入结束周" /> -->
          <el-select v-model="form.endWeek" placeholder="请选择开始周">
            <el-option label="1" value="1"/>
            <el-option label="2" value="2"/>
            <el-option label="3" value="3"/>
            <el-option label="4" value="4"/>
            <el-option label="5" value="5"/>
            <el-option label="6" value="6"/>
            <el-option label="7" value="7"/>
            <el-option label="8" value="8"/>
            <el-option label="9" value="9"/>
            <el-option label="10" value="10"/>
            <el-option label="11" value="11"/>
            <el-option label="12" value="12"/>
            <el-option label="13" value="13"/>
            <el-option label="14" value="14"/>
            <el-option label="15" value="15"/>
            <el-option label="16" value="16"/>
            <el-option label="17" value="17"/>
            <el-option label="18" value="18"/>
          </el-select>
        </el-form-item>
        <el-form-item label="上课节次" prop="classPeriodId">
          <!-- <el-input v-model="form.classPeriodId" placeholder="请输入上课节次id" /> -->
          <el-select v-model="form.classPeriodId" placeholder="请选择上课节次">
            <el-option label="1-2" value="1"/>
            <el-option label="3-5" value="2"/>
            <el-option label="6-7" value="3"/>
            <el-option label="8-9" value="4"/>
            <el-option label="10-12" value="5"/>
            <el-option label="13-15" value="6"/>
          </el-select>
        </el-form-item>
        <el-form-item label="申请原因描述" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入申请原因描述" />
        </el-form-item>
        <el-form-item label="创建日期" prop="createDate">
          <el-date-picker clearable v-model="form.createDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择创建日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改审核状态对话框 -->
    <el-dialog :title="title" :visible.sync="statusOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核状态" prop="status">
          <!-- <el-input v-model="form.status" placeholder="请输入审核状态 0-未审核 1-通过 2-驳回 3-使用完毕" /> -->
          <el-select v-model="form.status" placeholder="请选择审核状态">
            <el-option label="未审核" value="0" />
            <el-option label="通过" value="1" />
            <el-option label="驳回" value="2" />
            <el-option label="使用完毕" value="3" />
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listLabrequest, getLabrequest, delLabrequest, addLabrequest, updateLabrequest } from "@/api/lab/labrequest";
import {listLaboratory} from "@/api/lab/laboratory";
import { getCurrent } from '@/api/lab/semester';

export default {
  name: "Labrequest",
  data() {
    return {
      selectedValues: {
        0: '未审核',
        1: '通过',
        2: '驳回',
        3: '使用完毕'
      },
      currentSemester: 1,
      // 实验室存储管理表格数据
      laboratoryList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实验室申请表格数据
      labrequestList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否弹出状态修改对话框
      statusOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        labId: null,
        createDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  async created() {
    await this.getList();
    await this.getAllList();
    await this.getCurrent();
    this.reset();
  },
  methods: {
    /** 查询实验室存储管理列表 */
    getAllList() {
      this.loading = true;
      listLaboratory(this.queryParams).then(response => {
        this.laboratoryList = response.rows;
        console.log(this.laboratoryList);
        // this.total = response.total;
        this.loading = false;
      });
    },
    //获取当前学期
    async getCurrent() {
      await getCurrent().then(res => {
        console.log(res)
        this.currentSemester = res.data.semesterId
      })
    },

    /** 查询实验室申请列表 */
    getList() {
      this.loading = true;
      listLabrequest(this.queryParams).then(response => {
        this.labrequestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.statusOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        requestId: null,
        labId: null,
        semesterId: this.currentSemester,
        startWeek: null,
        endWeek: null,
        classPeriodId: null,
        status: null,
        version: null,
        reason: null,
        createDate: null,
        updateTime: null
      };
      console.log("学期ID"+this.form.semesterId);
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.requestId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实验室申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const requestId = row.requestId || this.ids
      getLabrequest(requestId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改实验室申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.requestId != null) {
            updateLabrequest(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.statusOpen = false;
              this.getList();
            });
          } else {
            addLabrequest(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.statusOpen = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const requestIds = row.requestId || this.ids;
      this.$modal.confirm('是否确认删除实验室申请编号为"' + requestIds + '"的数据项？').then(function () {
        return delLabrequest(requestIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lab/labrequest/export', {
        ...this.queryParams
      }, `labrequest_${new Date().getTime()}.xlsx`)
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleUpdateStatus":
          this.handleUpdateStatus(row);
          break;
        default:
          break;
      }
    },
    /** 更改维修状态 */
    handleUpdateStatus: function (row) {
      this.reset();
      const requestId = row.requestId || this.ids
      getLabrequest(requestId).then(response => {
        this.form = response.data;
        this.statusOpen = true;
        this.title = "修改实验室申请";
      });
    }
  }
};
</script>
